package com.cloudcc.mobile.presenter;

import com.cloudcc.cloudframe.bus.DynamicEventList.SendCommentEvent;
import com.cloudcc.cloudframe.bus.DynamicEventList.SendDynamicEvent;
import com.cloudcc.cloudframe.bus.EventEngine;
import com.cloudcc.cloudframe.bus.EventList.DeleteCommentEvent;
import com.cloudcc.cloudframe.bus.EventList.DeleteDynamicEvent;
import com.cloudcc.cloudframe.bus.EventList.DynamicEvent;
import com.cloudcc.cloudframe.bus.EventList.DynamicSingleEvent;
import com.cloudcc.cloudframe.bus.EventList.FavotiteDynamicEvent;
import com.cloudcc.cloudframe.bus.EventList.LikeCommentEvent;
import com.cloudcc.cloudframe.bus.EventList.LikeDynamicEvent;
import com.cloudcc.cloudframe.bus.EventList.VoteDynamicEvent;
import com.cloudcc.cloudframe.model.param.DynamicParam;
import com.cloudcc.cloudframe.model.param.FavoriteParam;
import com.cloudcc.cloudframe.model.param.LikeCommentParam;
import com.cloudcc.cloudframe.model.param.LikeDynamicParam;
import com.cloudcc.cloudframe.model.param.RemoveCommentParam;
import com.cloudcc.cloudframe.model.param.RemoveDynamicParam;
import com.cloudcc.cloudframe.util.StringUtils;
import com.cloudcc.mobile.AppContext;
import com.cloudcc.mobile.dao.DynamicEngine;
import com.cloudcc.mobile.dao.impl.DynamicEngineImpl;
import com.cloudcc.mobile.entity.AddImageInfo;
import com.cloudcc.mobile.entity.VoteMicroPost;
import com.cloudcc.mobile.entity.addmicropost.AddMicroPostDMany;
import com.cloudcc.mobile.entity.comment.AddMicroComment;
import com.cloudcc.mobile.event.BeauEventList.DynamicListEvent;
import com.cloudcc.mobile.manager.RunTimeManager;

import java.util.List;
import java.util.Map;

/**
 * 动态
 * 
 * @author wangzhiqiangx
 * @date 2015-7-30 下午12:00:07
 * 
 */
public class DynamicControl extends BasePresenter {
	private String mEns = RunTimeManager.getInstance().getlanguage();
	private DynamicEngine mDynamicEngine;

	public DynamicControl() {
		super();
		mDynamicEngine = new DynamicEngineImpl();
	}

	/**
	 * 获得动态
	 *
	 *
	 * @param page
	 */
	public void getDynamic(int page, String queryType) {
		DynamicEvent event = new DynamicEvent();
		event.type = queryType;

		if (checkNoNet(event)) {
			return;
		}

		if (page < 0) {
			event.setOk(false);
			event.setMessage("输入非法!");
			EventEngine.post(event);
			return;
		}
		int skip = Math.abs((page - 1) * 10);
		DynamicParam param = new DynamicParam();
		param.feedSort = "lastModifyDate";
		param.limit = 10;
		param.skip = skip;
		param.queryType = queryType;

		// 判断serverName
		String dynamicType = RunTimeManager.getInstance().getDynamicType();
		String serviceName = StringUtils.EMPTY;
		if (StringUtils.equals(dynamicType, RunTimeManager.DynamicType.FOLLOW)) {
			serviceName = "getMyChatter01";
		} else {
			serviceName = "getChatters01";
		}
		mDynamicEngine.getDynamics(serviceName, param, event);
	}

	/**
	 * 获得关于某一个用户的动态
	 * 
	 * @param page
	 * @param userId
	 */
	public void getDynamicForOne(int page, String userId) {
		DynamicEvent event = new DynamicEvent();
		event.type = "self";
		if (checkNoNet(event)) {
			return;
		}
		if (page < 0) {
			event.setOk(false);
			event.setMessage("输入非法!");
			EventEngine.post(event);
			return;
		}
		int skip = Math.abs((page - 1) * 10);
		DynamicParam param = new DynamicParam();
		param.feedSort = "lastModifyDate";
		param.userId = userId;
//		param.limit = 10;
		param.skip = skip;
		param.queryType = "me";
		String serviceName = "getChatters01";
		mDynamicEngine.getDynamics(serviceName, param, event);
	}

	/**
	 * 获取记录的 相关微贴列表
	 * 
	 * @param page
	 * @param recordId
	 */
	public void getDynamicsAboutRecord(int page, String recordId) {
		DynamicListEvent event = new DynamicListEvent();

		if (page < 0) {
			event.setOk(false);
			event.setMessage("输入非法!");
			EventEngine.post(event);
			return;
		}
		int skip = Math.abs((page - 1) * 10);
		DynamicParam param = new DynamicParam();
		param.feedSort = "lastModifyDate";
		param.limit = 10;
		param.skip = skip;
		param.queryType = "record";
		param.recordId = recordId;
		mDynamicEngine.getDynamicsAboutRecord(param, event);
	}

	/**
	 * 收藏
	 * 
	 * @param id
	 * @param ops
	 */
	public void Favorite(String id, boolean ops) {
		FavotiteDynamicEvent event = new FavotiteDynamicEvent();
		FavoriteParam param = new FavoriteParam();
		param.feedId = id;
		param.favoriteOrUnfavorite = ops ? "1" : "-1";
		mDynamicEngine.FavoriteDynamic(param, event);
	}

	/**
	 * 喜欢 某条动态
	 * 
	 * @param id
	 * @param ops
	 */
	public void like(String id, boolean ops) {
		LikeDynamicEvent event = new LikeDynamicEvent();
		LikeDynamicParam param = new LikeDynamicParam();
		param.feedId = id;
		param.likeOrDislike = ops ? "1" : "-1";
		mDynamicEngine.LikeDynamic(param, event);
	}

	/**
	 * 喜欢 某条 评论
	 * 
	 * @param id
	 * @param ops
	 */
	public void likeComment(String id, boolean ops) {
		LikeCommentEvent event = new LikeCommentEvent();
		LikeCommentParam param = new LikeCommentParam();
		param.commentId = id;
		param.likeOrDislike = ops ? "1" : "-1";
		mDynamicEngine.LikeComment(param, event);
	}

	/**
	 * 删除 自己发布的动态
	 * 
	 * @param id
	 */
	public void delete(String id) {
		DeleteDynamicEvent event = new DeleteDynamicEvent();
		RemoveDynamicParam param = new RemoveDynamicParam();
		param.feedId = id;
		mDynamicEngine.deleteDynamic(param, event);
	}

	/**
	 * 删除自己发布的评论
	 * 
	 * @param id
	 */
	public void deleteComment(String id) {
		DeleteCommentEvent event = new DeleteCommentEvent();
		RemoveCommentParam param = new RemoveCommentParam();
		param.commentId = id;
		mDynamicEngine.deleteComment(param, event);
	}

	public void getDynamic(String feedId) {
		DynamicSingleEvent event = new DynamicSingleEvent();
		DynamicParam param = new DynamicParam();

		param.feedId = feedId;
		param.limit = 1;
		param.skip = 0;
		param.feedSort = AppContext.feedSort;
		param.queryType = "findone";
		mDynamicEngine.getDynamic(param, event);
	}

	/**
	 * 发表评论
	 * 
	 * @param content
	 * @param dynamicId
	 */
	public void sendComment(String content, String dynamicId) {

		SendCommentEvent event = new SendCommentEvent();
		if (StringUtils.isBlank(content)) {
			event.setOk(false);
			if ("en".equals(mEns)) {
				event.setMessage("评论内容不能为空");
			}else{
				event.setMessage("评论内容不能为空");
		}
			
			EventEngine.post(event);
		}
		AddMicroComment param = new AddMicroComment();
		param.setBody(content);
		param.setFeedid(dynamicId);
		mDynamicEngine.sendComment(param, event);
	}

	public <T> void sendCommonDynamic(T param, String serviceName) {
		SendDynamicEvent event = new SendDynamicEvent();
		if (checkNoNet(event)) {
			return;
		}
		mDynamicEngine.sendDynamic(param, event, serviceName);
	}

	/**
	 * 发送带图片的微贴 步骤是 1、上传图片 如果上传成功就发布帖子 2、发布帖子
	 */
	public void sendPhotoDynamic(List<AddImageInfo> images,
			final AddMicroPostDMany param) {
		SendDynamicEvent event = new SendDynamicEvent();
		if (checkNoNet(event)) {
			return;
		}
		mDynamicEngine.uploadPhoto(images, param, event);
	}

	/**
	 * 投票
	 * 
	 * @param dynamicId
	 * @param opt
	 */
	public void sendVote(String dynamicId, String opt) {
		VoteDynamicEvent event = new VoteDynamicEvent();

		VoteMicroPost vPost = new VoteMicroPost();
		vPost.setFeedId(dynamicId);
		vPost.setOption(opt);
		mDynamicEngine.voteDynamic(vPost, event);
	}

	/**
	 * 发送带图片的巡检 步骤是 1、上传图片 如果上传成功就发布帖子 2、发布帖子
	 */
	public void sendPhotoBull(List<AddImageInfo> images,Map<String,String> json,String isxunjian) {
		SendDynamicEvent event = new SendDynamicEvent();
		if (checkNoNet(event)) {
			return;
		}
		mDynamicEngine.bullUploadPhoto(images, json, event,isxunjian);
	}

	/**
	 * 发送带图片的微贴 步骤是 1、上传图片 如果上传成功就发布帖子 2、发布帖子
	 */
	public void sendPhotoKaoqin(List<AddImageInfo> images) {
		SendDynamicEvent event = new SendDynamicEvent();
		if (checkNoNet(event)) {
			return;
		}
		mDynamicEngine.uploadPhotoKaoqin(images, event);
	}

	/**
	 * 发送带图片的巡检 步骤是 1、上传图片 如果上传成功就发布帖子 2、发布帖子
	 */
	public void sendDataBull(Map<String,String> json,String isxunjian) {
		SendDynamicEvent event = new SendDynamicEvent();
		if (checkNoNet(event)) {
			return;
		}
		mDynamicEngine.bullSendData(json, event,isxunjian);
	}
}
